<template>
	<view class="container">
		<z-paging ref="paging" v-model="billList" @query="queryList" :auto="false">
			<view slot="top">
				<view style="background-color: #ffffff;">
					<view class="search-box">
						<u-search placeholder="请输入关键字搜索" v-model="formData.kw" :show-action="false" :clearabled="true"
							:animation="false" shape="round" bgColor="#F5F6FA" height="72rpx" searchIconColor="#999999"
							placeholderColor="#999999" @change="$u.debounce(refreshList, 500)" :inputStyle="{
								color: '#333333',
								backgroundColor: '#F5F6FA',
							}"></u-search>
					</view>
					<view class="sort-box">
						<view class="sort-item" @click="handleSort('room_name')">
							<text>房号</text>
							<image src="/pages-landlord/static/room/sort-no.png" v-if="sortConfig.sort!='room_name'">
							</image>
							<image src="/pages-landlord/static/room/sort-up.png"
								v-else-if="sortConfig.sort=='room_name' && sortConfig.way=='asc'"></image>
							<image src="/pages-landlord/static/room/sort-down.png"
								v-else-if="sortConfig.sort=='room_name' && sortConfig.way=='desc'"></image>
						</view>
						<view class="sort-item" @click="handleSort('day')">
							<text>账期</text>
							<image src="/pages-landlord/static/room/sort-no.png" v-if="sortConfig.sort!='day'">
							</image>
							<image src="/pages-landlord/static/room/sort-up.png"
								v-else-if="sortConfig.sort=='day' && sortConfig.way=='asc'"></image>
							<image src="/pages-landlord/static/room/sort-down.png"
								v-else-if="sortConfig.sort=='day' && sortConfig.way=='desc'"></image>
						</view>
						<view class="sort-item" @click="handleSort('money')">
							<text>金额</text>
							<image src="/pages-landlord/static/room/sort-no.png" v-if="sortConfig.sort!='money'">
							</image>
							<image src="/pages-landlord/static/room/sort-up.png"
								v-else-if="sortConfig.sort=='money' && sortConfig.way=='asc'"></image>
							<image src="/pages-landlord/static/room/sort-down.png"
								v-else-if="sortConfig.sort=='money' && sortConfig.way=='desc'"></image>
						</view>
					</view>
					<!-- tab切换 -->
					<u-tabs :list="tabsList" :current="tabsIndex" :scrollable="false" @change="tabsChange"
						lineWidth="20" lineColor="#f56c6c" :activeStyle="{
							color: '#000',
							fontWeight: 'bold',
							transform: 'scale(1.05)'
						}" lineHeight="4"></u-tabs>
				</view>
			</view>
			<u-checkbox-group shape="circle" activeColor="#fe4f04" v-model="selectbillList">
				<view class="room-list">
					<view v-for="(item, index) in billList" :key="index" class="room-item">
						<view class="room-item-con" @click="jump('/pages-landlord/house/billNew?id='+item.id)">
							<view class="checkbox-group" v-if="showSelect">
								<u-checkbox :name="item.id"></u-checkbox>
							</view>
							<view class="room-info">
								<view class="room-number">{{item.room_name}}</view>
								<view class="room-address">{{item.build_name}}</view>
								<view class="room-details">{{item.user_name}} {{item.user_phone}}</view>
								<view class="t3">
									{{item.remark}}
									<text class="font-color-red t4"
										v-if="item.overdue_day>0">已逾期{{item.overdue_day}}天</text>
									<text class="font-color-green t4" v-if="item.pay_time">支付时间：{{item.pay_time}}</text>
								</view>
							</view>
							<view class="room-status">
								<view class="price">
									<text class="current-price">￥{{item.money}}</text>
								</view>
								<view class="date">{{item.day}}</view>
								<view class="status-tag" :class="item.status===2?'font-color-green':''">
									<text>
										<!-- 0-待审核 1-待支付 2-已支付 -->
										{{item.status===0?'待审核':item.status===1?'待收款':item.status===2?'已收款':''}}
									</text>
								</view>
							</view>
						</view>
						<view class="room-bottom">
							<template v-if="tabsIndex===1">
								<view class="btn btn-red" @click="audit(item.id)">审核</view>
								<view class="btn" @click="del(item.id)">删除</view>
							</template>
							<template v-if="tabsIndex===2">
								<view class="btn" @click="sms(item.id)">短信</view>
								<view class="btn" @click="call(item.user_phone)">拨号</view>
								<view class="btn btn-red" @click="receive_payment(item.id)">收款</view>
							</template>
							<template v-if="tabsIndex===3">
								<view class="btn" @click="revoke(item.id)">撤回</view>
							</template>
						</view>
					</view>
				</view>
			</u-checkbox-group>
			<view slot="bottom">
				<view class="safe-bottom action-bar" v-if="showSelect">
					<view class="radio-group">
						<image src="/pages-landlord/static/room/icon-xuan.png" v-if="isAllSelect"
							@click="changeAllSelect"></image>
						<image src="/pages-landlord/static/room/icon-xuan1.png" v-if="!isAllSelect"
							@click="changeAllSelect"></image>
					</view>
					<view class="action-item" @click="handleAudit" v-if="tabsIndex===1||tabsIndex===0">审核</view>
					<view class="action-item" @click="handlePay" v-if="tabsIndex===2">收款</view>
					<view class="action-item" @click="handleRevoke" v-if="tabsIndex===3">撤回</view>
					<view class="action-item" @click="handleDelete" v-if="tabsIndex!==3">删除</view>
					<view class="action-item" @click="showSelect=false">取消</view>
				</view>
				<view class="safe-bottom action-bar" v-if="!showSelect">
					<view class="action-item" @click="handleBatch">批量</view>
					<view class="action-item" @click="handleAll">{{buildingName}}</view>
					<view class="action-item" @click="handleFilter">筛选</view>
				</view>
			</view>
		</z-paging>
		<u-popup :show="showPopup" mode="bottom" :closeOnClickOverlay="false" round="40rpx">
			<view class="filter-modal">
				<view class="header">
					<text @click="handleCancel">取消</text>
					<text style="font-weight: bold">筛选</text>
					<text class="confirm" @click="handleConfirm">确认</text>
				</view>
				<view class="fill-box"></view>
				<view class="section-box">
					<view class="title">日期</view>
					<view class="tags-box">
						<view :class="formData.date_type==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in date_types" :key="index" @click="handledate_type(item)">
							{{item.text}}
						</view>
					</view>
				</view>
				<view class="section-box">
					<view class="title">指定</view>
					<view class="date-range">
						<view class="date-item" @click="showStartDate= true">
							<view v-if="formData.start">{{formData.start}}</view>
							<view class="placeholder" v-if="!formData.start">请选择</view>
						</view>
						<text>至</text>
						<view class="date-item" @click="showEndDate= true">
							<view v-if="formData.end">{{formData.end}}</view>
							<view class="placeholder" v-if="!formData.end">请选择</view>
						</view>
					</view>
				</view>
				<!-- <view class="section-box">
					<view class="title">提前</view>
					<view class="input-box">
						<input type="number" placeholder="0:表示关闭提醒" v-model="formData.remindDay" />
						<text>提醒</text>
					</view>
				</view> -->

				<!-- <view class="section-box">
					<view class="title">类型</view>
					<view class="tags-box">
						<view :class="formData.propertyType==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in propertyTypes" :key="index" @click="handlePropertyType(item)">
							{{item.text}}
						</view>
					</view>
				</view> -->
				<!-- <view class="section-box">
					<view class="title">状态</view>
					<view class="tags-box">
						<view :class="formData.status==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in statusOptions" :key="index" @click="handleStatus(item)">
							{{item.text}}
						</view>
					</view>
				</view> -->
				<!-- <view class="section-box">
					<view class="title">业务员</view>
					<view class="tags-box">
						<view :class="formData.personType==item.value?'tag-item active':'tag-item'"
							v-for="(item, index) in personTypes" :key="index" @click="handleRoomType(item)">
							{{item.text}}
						</view>
					</view>
				</view> -->
			</view>
			<u-datetime-picker :show="showStartDate" v-model="startDate" mode="date" @confirm="onStartDateConfirm"
				@cancel="onStartDateCanceel" />
			<u-datetime-picker :show="showEndDate" v-model="endDate" mode="date" @confirm="onEndDateConfirm"
				@cancel="onEndDateCanceel" />
		</u-popup>
	</view>
</template>
<script>
	import {
		getBill,
		index_statBill,
		billDel,
		billAudit,
		receive_payment,
		billSms,
		billRevoke
	} from '@/api/landlord.js'
	export default {
		data() {
			return {
				sortConfig: {
					sort: '', //排序，房号：room_name，账期：day，金额：money
					way: '' // 'asc' | 'desc' | ''
				},
				buildingName: '全部大楼',
				tabsList: [{
					id: 0,
					name: '全部'
				}, {
					id: 1,
					name: '待审'
				}, {
					id: 2,
					name: '待收'
				}, {
					id: 3,
					name: "已收"
				}],
				tabsIndex: 0,
				showSelect: false,
				selectbillList: [],
				isAllSelect: false,
				billList: [],
				showPopup: false,
				showStartDate: false,
				showEndDate: false,
				originalKw: '',
				formData: {
					status: '', //状态：0-待审核，1-待支付，2-已支付，全部传空
					kw: '',
					room_id: '',
					propertyType: '',
					start: '',
					end: '',
					date_type: '' // 日期，1-今天，2-本周，3-本月，4-今年
				},
				startDate: new Date(),
				endDate: new Date(),
				date_types: [{
					text: '今天',
					value: 1
				}, {
					text: '本周',
					value: 2
				}, {
					text: '本月',
					value: 3
				}, {
					text: '今年',
					value: 4
				}],
				propertyTypes: [{
					text: '全部',
					value: 1
				}, {
					text: '出租',
					value: 2
				}, {
					text: '普通',
					value: 3
				}, {
					text: '退房',
					value: 4
				}],
				statusOptions: [{
					text: '全部',
					value: 0
				}, {
					text: '待审',
					value: 1
				}, {
					text: '代收',
					value: 2
				}, {
					text: '已收',
					value: 3
				}],
				personTypes: [{
					text: 'TT',
					value: 1
				}, {
					text: '22',
					value: 2
				}, {
					text: '1室0厅1卫',
					value: 3
				}, {
					text: '3室1厅',
					value: 4
				}, {
					text: '4室1厅',
					value: 5
				}],
				total: 0
			};
		},
		
		onLoad(options) {
			if (options.kw) {
				this.formData.kw = options.kw
			}
			if (options.room_id) {
				this.formData.room_id = options.room_id
				this.originalKw = options.kw || ''
			}
			if (options.building_id) {
				this.formData.build_id = options.building_id
				this.buildingName = options.building_name || '全部大楼'
			}
			if (options.status) {
				this.formData.status = options.status
				this.tabsIndex = Number(options.status)
			}
			if (options.start) {
				this.formData.start = options.start
			}
		},
		onUnload() {
			uni.$off('selectBuilding'); // 清除事件监听
		},
		onShow() {
			uni.$on('selectBuilding', (building) => {
				this.formData.build_id = building.id
				this.buildingName = building.name
			});
			this.refreshList();
		},
		watch: {
			selectbillList(newVal) {
				if (newVal.length) {
					this.isAllSelect = newVal.length === this.billList.length;
				}
			},
			'formData.kw'(val) {
				if (val !== this.originalKw) {
					this.formData.room_id = 0
				}
			}
		},
		methods: {
			handledate_type(item) {
				this.formData.date_type = item.value;
			},
			handleStatus(item) {
				this.formData.status = item.value;
			},
			handlePropertyType(item) {
				this.formData.propertyType = item.value;
			},
			handleRoomType(item) {
				this.formData.roomType = item.value;
			},
			onStartDateConfirm(event) {
				this.startDate = event.value
				//格式化时间戳YYYY-MM-DD
				this.formData.start = uni.$u.timeFormat(event.value, 'yyyy-mm-dd')
				this.showStartDate = false
			},
			onStartDateCanceel() {
				this.startDate = ''
				this.formData.start = ''
				this.showStartDate = false
			},
			onEndDateConfirm(event) {
				this.endDate = event.value
				//格式化时间戳YYYY-MM-DD
				this.formData.end = uni.$u.timeFormat(event.value, 'yyyy-mm-dd')
				this.showEndDate = false
			},
			onEndDateCanceel() {
				this.endDate = ''
				this.formData.end = ''
				this.showEndDate = false
			},
			handleCancel() {
				this.startDate = ''
				this.endDate = ''
				this.formData.start = ''
				this.formData.end = ''
				this.formData.date_type = ''
				this.showPopup = false
			},
			handleConfirm() {
				this.showPopup = false
				this.refreshList()
			},
			getTabsListCount() {
				index_statBill(this.formData, ).then(res => {
					let data = res.data.data
					this.tabsList[0].name = `全部(${data.all})`
					this.tabsList[1].name = `待审(${data.dai_shen})`
					this.tabsList[2].name = `待收(${data.dai_shou})`
					this.tabsList[3].name = `已收(${data.yi_shou})`
				})
			},

			refreshList() {
				if (this.$refs.paging) {
					this.$refs.paging.refresh();
					this.getTabsListCount();
				}
			},
			queryList(pageNo, pageSize) {
				getBill({
					...this.formData,
					...this.sortConfig,
					page: pageNo,
					page_size: pageSize
				}).then(res => {
					this.total = res.data.data.total
					this.$refs.paging.complete(res.data.data.data);
				}).catch(res => {
					this.$refs.paging.complete(false);
				})
			},
			handleSort(sort) {
				if (this.sortConfig.sort === sort) {
					this.sortConfig.way = this.sortConfig.way === 'asc' ? 'desc' : 'asc';
				} else {
					this.sortConfig.sort = sort;
					this.sortConfig.way = 'asc';
				}
				this.refreshList()
			},
			tabsChange(e) {
				this.showSelect = false;
				this.billList = []
				this.selectbillList = [];
				this.tabsIndex = e.index
				this.formData.status = e.index === 0 ? '' : Number(e.index - 1)
				this.refreshList()
			},
			checkboxChange(n) {
				console.log(n, n.length, this.billList.length);
				// if(this.billList.length=n.length){
				// 	this.isAllSelect=true
				// }else{
				// 	this.isAllSelect=false
				// }
			},
			handleBatch() {
				this.showSelect = true;
				this.selectbillList = [];
			},
			handleAll() {
				this.$Tips('/pages-landlord/select-building/select-building')
			},
			handleFilter() {
				console.log('筛选');
				this.showPopup = true
			},
			changeAllSelect() {
				this.isAllSelect = !this.isAllSelect;
				if (this.isAllSelect) {
					this.selectbillList = this.billList.map(option => option.id);
				} else {
					this.selectbillList = [];
				}
			},
			handleAudit() {
				let that = this
				console.log('审核');
				if (this.selectbillList.length == 0) {
					return this.$Toast('未选择账单')
				}
				uni.showModal({
					confirmColor: "#FE4F04",
					content: "确定审核所选账单吗？",
					success(res) {
						if (res.confirm) {
							let ids = that.selectbillList.join(',')
							that.audit(ids)
						}
					}
				})
			},
			handlePay() {
				let that = this
				console.log('收款');
				if (this.selectbillList.length == 0) {
					return this.$Toast('未选择账单')
				}
				uni.showModal({
					confirmColor: "#FE4F04",
					content: "确定收款所选账单吗？",
					success(res) {
						if (res.confirm) {
							let ids = that.selectbillList.join(',')
							that.receive_payment(ids)
						}
					}
				})
			},
			handleRevoke() {
				console.log('撤回');
				let that = this
				if (this.selectbillList.length == 0) {
					return this.$Toast('未选择账单')
				}
				uni.showModal({
					confirmColor: "#FE4F04",
					content: "确定撤回所选账单吗？",
					success(res) {
						if (res.confirm) {
							let ids = that.selectbillList.join(',')
							that.revoke(ids)
						}
					}
				})
			},
			handleDelete() {
				console.log('删除');
				let that = this
				if (this.selectbillList.length == 0) {
					return this.$Toast('未选择账单')
				}
				uni.showModal({
					confirmColor: "#FE4F04",
					content: "确定删除所选账单吗？",
					success(res) {
						if (res.confirm) {
							let ids = that.selectbillList.join(',')
							that.del(ids)
						}
					}
				})
			},
			handleSuccess() {
				this.$Toast('操作成功')
				this.refreshList()
			},
			//审核
			audit(ids) {
				billAudit({
					ids: ids
				}).then(res => {
					this.handleSuccess()
				})
			},
			del(ids) {
				billDel({
					ids: ids
				}).then(res => {
					this.handleSuccess()
				})
			},
			sms(id) {
				billSms({
					id: id
				}).then(res => {
					this.handleSuccess()
				})
			},
			call(phone) {
				uni.makePhoneCall({
					phoneNumber: phone,
					success: function() {
						console.log('拨打电话成功！');
					},
					fail: function(err) {
						console.error('拨打电话失败：', err);
					}
				});
			},
			receive_payment(ids) {
				receive_payment({
					ids: ids
				}).then(res => {
					this.handleSuccess()
				})
			},
			revoke(ids) {
				billRevoke({
					ids: ids
				}).then(res => {
					this.handleSuccess()
				})
			},
			// 路由跳转
			jump(url) {
				uni.navigateTo({
					url: url
				});
			},
		}
	};
</script>
<style lang="scss" scoped>
	.filter-modal {
		background: #fff;
		border-radius: 40rpx 40rpx 0 0;
		overflow-y: scroll;

		.header {
			position: fixed;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 40rpx;
			box-sizing: border-box;
			height: 120rpx;
			border-radius: 40rpx 40rpx 0 0;
			border-bottom: 1px solid #eee;
			background: #ffffff;
			z-index: 100;

			.confirm {
				color: #FE4F04;
			}
		}

		.fill-box {
			height: 120rpx;
		}

		.section-box {
			padding: 0 20rpx;
			margin: 20rpx 0 40rpx;

			.title {
				font-size: 28rpx;
				color: #222222;
				font-weight: 600;
				margin-bottom: 20rpx;
			}

		}

		.tags-box {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;

			.tag-item {
				font-size: 28rpx;
				height: 68rpx;
				line-height: 68rpx;
				padding: 0rpx 50rpx;
				border-radius: 40rpx;
				background: #F5F6FA;
				border: 1rpx solid #F5F6FA;
			}

			.tag-item.active {
				color: #FF7D41;
				background: #FFF6ED;
				border: 1rpx solid #FE4F04;
			}
		}

		.date-range,
		.input-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			gap: 20rpx;

			input {
				flex: 1;
				height: 68rpx;
				background: #F5F6FA;
				border-radius: 40rpx;
				line-height: 68rpx;
				padding: 0 20rpx;
				margin-right: 20rpx;
			}

			.date-item {
				width: 302rpx;
				height: 68rpx;
				background: #F5F6FA;
				border-radius: 40rpx;
				line-height: 68rpx;
				text-align: center;

				.placeholder {
					font-size: 28rpx;
					color: #999999;
				}
			}

			text {
				font-weight: 400;
				font-size: 28rpx;
				color: #222222;
				text-align: center;
			}
		}
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
		background-color: #F5F6F7;
	}

	.search-box {
		padding: 24rpx 32rpx;

		:deep(.u-search) {
			background-color: #ffffff !important;
			border-radius: 36rpx;
			box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.05);

			.u-search__content {
				background-color: #ffffff !important;
				height: 72rpx;
				padding: 0 30rpx;

				.u-search__content__icon {
					margin-right: 8rpx;
				}

				.u-search__content__input {
					font-size: 28rpx;
				}
			}
		}
	}

	.sort-box {
		padding: 0 32rpx 32rpx;
		display: flex;
		gap: 26rpx;
		background-color: #fff;

		.add-btn {
			padding: 20rpx;
		}

		.sort-item {
			flex: 1;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 18rpx 24rpx;
			background: #F5F6FA;
			border-radius: 12rpx;
			font-weight: 500;
			font-size: 26rpx;
			color: #202020;
			line-height: 36rpx;

			text {
				font-weight: 500;
				font-size: 26rpx;
				color: #202020;
			}

			image {
				width: 12rpx;
				height: 24rpx;
			}
		}
	}

	.font-color-red {
		color: #FF0000 !important;
	}

	.font-color-green {
		color: #2FAA4E;
	}

	.btn-red {
		color: #FF0000;
		border-color: #FF0000 !important;
	}

	.room-list {
		flex: 1;
		overflow: auto;
		padding: 20rpx;

		.room-item {
			background-color: #FFFFFF;
			padding: 30rpx;
			border-radius: 12rpx;
			margin-bottom: 20rpx;

			.room-item-con {
				display: flex;
				align-items: center;


				.checkbox-group {
					margin-right: 32rpx;
				}

				.radio-group {
					display: flex;
					align-items: center;
					width: 70rpx;

					image {
						width: 36rpx;
						height: 36rpx;
					}
				}

				.room-info {
					flex: 1;

					.t3 {
						margin-top: 12rpx;
						font-size: 26rpx;
						color: #333333;

						.t4 {
							margin-left: 10rpx;
						}
					}

				}

				.room-number {
					font-size: 30rpx;
					color: #333333;
					font-weight: 500;
					margin-bottom: 12rpx;
				}

				.room-address {
					font-size: 26rpx;
					color: #444444;
					margin-bottom: 8rpx;
				}

				.room-details {
					font-size: 20rpx;
					color: #999999;
				}

				.room-status {
					text-align: right;
				}

				.price {
					margin-bottom: 8rpx;
				}

				.original-price {
					font-size: 24rpx;
					color: #D1D2D4;
					text-decoration: line-through;
					margin-right: 8rpx;
				}

				.current-price {
					font-size: 28rpx;
					color: #FE4F04;
					font-weight: 500;
				}

				.date {
					font-size: 26rpx;
					color: #666666;
					margin-bottom: 8rpx;
				}


				.status-tag {
					display: inline-flex;
					align-items: center;
					background-color: #F0F9EB;
					padding: 4rpx 12rpx;
					border-radius: 4rpx;
				}

				.status-tag text {
					font-size: 12px;
					color: #67C23A;
					margin-left: 4rpx;
				}
			}
		}

		.room-bottom {
			display: flex;
			justify-content: flex-end;
			margin-top: 20rpx;

			.btn {
				border: 1px solid #D1D2D4;
				border-radius: 28rpx;
				margin-left: 16rpx;
				padding: 8rpx 32rpx;

			}
		}
	}

	.action-bar {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 20rpx 32rpx;
		background-color: #FFFFFF;

		.radio-group {
			display: flex;
			align-items: center;
			width: 70rpx;

			image {
				width: 36rpx;
				height: 36rpx;
			}
		}

		.action-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			flex: 1;
			text-align: center;
		}
	}
</style>